package Prototype;


import java.util.Hashtable;

public class ShapeCache {

    private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();

    public static Shape getShape(String shapeId){
        Shape cachedShape = shapeMap.get(shapeId); // 根据shapeId从shapeMap里取出shape对象的clone()对象

        return (Shape) cachedShape.clone();  // 相当于new了一个新对象
        // 此处若 (obj = cachedShape ) 返回 obj,实际上是一个对象. obj指向 cachedShape 的引用
    }

    // 对每种形状都运行数据库查询,并创建该形状
    public static void loadCache(){
        Rectangle rectangle = new Rectangle();
        rectangle.setId("1");
        shapeMap.put(rectangle.getId(), rectangle);

        Triangle triangle = new Triangle();
        triangle.setId("2");
        shapeMap.put(triangle.getId(), triangle);

        Square square = new Square();
        square.setId("3");
        shapeMap.put(square.getId(), square);
    }


}
